package com.cyzy.controller;


import com.cyzy.dto.Result;
import com.cyzy.pojo.TrainFriends;
import com.cyzy.service.FriendsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/friends")
public class FriendsController {

    @Autowired
    private FriendsService friendsService;

    //查询乘车人列表
    @GetMapping("/select")
    public Result getFriendList(String userId){
        List<TrainFriends> friendList = friendsService.getFriendList(userId);
        return Result.ok().message("查询成功").data("data", friendList);
    }

    //根据亲友名字查询
    @GetMapping("/selectByName")
    public Result getFriendListByName(String friendName){
        List<TrainFriends> friends = friendsService.selectByName(friendName);
        return Result.ok().message("查询成功").data("data", friends);
    }

    //根据亲友ID查询信息  修改回显时候使用
    @GetMapping("/selectById")
    public Result selectById(String friendId){
        TrainFriends trainFriends = friendsService.selectById(friendId);
        return Result.ok().message("查询成功").data("data", trainFriends);
    }


    //增加乘车人信息
    @PostMapping("/add")
    public Result addFriend(TrainFriends trainFriends){
        int i = friendsService.addFriends(trainFriends);
        if(i == 0){
            return Result.error().message("新增失败");
        }
        return Result.ok().message("新增成功").data("data", i);
    }

    //删除亲友
    @DeleteMapping("/delete")
    public Result deleteFriend( TrainFriends trainFriends){
        int i = friendsService.deleteFriends(trainFriends);
        if(i == 0){
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功").data("data", i);
    }

    //批量删除亲友
    @DeleteMapping("/deletes")
    public Result deletes(@RequestBody List<Long> friendId ){
        int i = friendsService.deletes(friendId.toArray(new Long[0]));
        if(i == 0){
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功").data("data", i);
    }

    //修改亲友信息
    @PutMapping("/update")
    public Result updateFriend(@RequestBody TrainFriends trainFriends){

        int i = friendsService.updateFriends(trainFriends);
        if(i == 0){
            return Result.error().message("修改失败");
        }
        return Result.ok().message("修改成功").data("data", i);
    }
}
